#include "CGroupItem.h"
#include <memory>
#include "CPropertyEditor.h"
#include <algorithm>

//-------------------------------------

CGroupItem::CGroupItem( CPropertyEditor * _owner, const QString & _name )
    : owner(_owner ),
      name( _name )
{
}

//-------------------------------------

void CGroupItem::Map( std::vector<CPropertyEditorItem*> & v )
{
    if( !items.empty() )
    {
        //v.push_back(this);
        typedef std::vector<CPropertyItem*> Children;
        Children children( items.begin(), items.end() );
        std::stable_sort( children.begin(), children.end(), LessPropItem() );
        for( Children::iterator i( children.begin() ), last( children.end() ); i!=last; ++i )
            ( *i )->Map(v);
    }
}

//-------------------------------------

void CGroupItem::AddItem( CPropertyItem * item )
{
    items.insert( item );
    item->SetGroup( this );
    Editor()->Update();
}

//-------------------------------------

void CGroupItem::DeleteItem( const QString & key )
{
    std::auto_ptr<CPropertyItem> item( new CPropertyItem( key, "" ) );
    items.erase( item.get() );
}

//-------------------------------------

CPropertyItem * CGroupItem::GetItem( const QString & key )
{
    std::auto_ptr<CPropertyItem> item( new CPropertyItem( key, "" ) );
    typedef std::set<CPropertyItem*, LessPropItem >::iterator Iter;
    Iter i ( items.find( item.get() ) );
    if( i == items.end() )
        return 0;
    return *i;
}

//-------------------------------------

